/*
 * @filename WebUserDetailsService.java
 * @author barry
 * @version 0.0.1
 * @date 2020年2月20日
 */
package com.bnzj.core.web.security;

import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import reactor.core.publisher.Mono;

/**
 * @author barry
 * @date 2020-02-20
 */
public interface WebUserDetailsService extends UserDetailsService, ReactiveUserDetailsService {

    @Override
    default Mono<UserDetails> findByUsername(String username) {
        UserDetails user = getByUsername(username);
        if (null == user) {
            return Mono.error(new UsernameNotFoundException("User " + username + " Not Found"));
        }
        return Mono.just(user);
    }

    @Override
    default UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserDetails user = getByUsername(username);
        if (null == user) {
            throw new UsernameNotFoundException("User " + username + " not found");
        }
        return user;
    }

    SecurityUser getByUsername(String username);
}
